import Cache from '@/utils/cache';
import {
	UID,
	TOKEN,
	USER_INFO,
} from '@/config/cache';

const state = {
	token: Cache.get(TOKEN) || '',
	userInfo: Cache.get(USER_INFO) ? JSON.parse(Cache.get(USER_INFO)) : null,
	uid: Cache.get(UID) || null,
};

const mutations = {
	//用户登录存储Token
	LOGIN(state, opt) {
		state.token = opt.token;
		Cache.set(TOKEN, opt.token);
	},
	//获取并存储用户Id
	SETUID(state, val) {
		state.uid = val;
		Cache.set(UID, val);
	},
	//退出登录
	LOGOUT(state) {
		state.uid = undefined
		state.token = undefined;
		state.userInfo = undefined
		Cache.clear(UID);
		Cache.clear(TOKEN);
		Cache.clear(USER_INFO);
	},
	//获取并存储用户信息
	UPDATE_USERINFO(state, userInfo) {
		state.userInfo = userInfo;
		Cache.set(USER_INFO, userInfo);
	}
};


export default {
	state,
	mutations
};